package module.service.impl;

import common.bean.SearchPageResult;
import common.bean.SearchParameter;
import module.dao.UserDao;
import module.model.UserModel;
import module.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Transactional
    public UserModel addUser(UserModel model) {
        Boolean b1 = this.countUserNameOrUserMobile(model.getUserName(), null);//判断是否存在用户名
        Boolean b2 = this.countUserNameOrUserMobile(model.getUserMobile(), null);//判断是否存在手机号码
        if(b1 && b2){
            model.setCreateDate(new Date());
            userDao.save(model);
            //model.setCreateUser(model.getUserId());
            //userDao.update(model);
            return model;
        }
        return null;
    }
    @Transactional
    public UserModel updateUser(UserModel model, Long updateUserId) {
        Long userId = model.getUserId();
        if(userId==null){return null;}
        Boolean b1 = this.countUserNameOrUserMobile(model.getUserName(), userId);//判断是否存在用户名
        Boolean b2 = this.countUserNameOrUserMobile(model.getUserMobile(), userId);//判断是否存在手机号码
        if(b1 && b2){
            UserModel user = userDao.get(UserModel.class, userId);
            if(user==null){return null;}
            if(StringUtils.isNotBlank(model.getUserName())){
                user.setUserName(model.getUserName());
            }
            if(StringUtils.isNotBlank(model.getUserMobile())){
                user.setUserMobile(model.getUserMobile());
            }
            if(StringUtils.isNotBlank(model.getUserEmail())){
                user.setUserEmail(model.getUserEmail());
            }
            if(StringUtils.isNotBlank(model.getIdCard())){
                user.setIdCard(model.getIdCard());
            }
            if(StringUtils.isNotBlank(model.getIdCard())){
                user.setIdCard(model.getIdCard());
            }
            user.setRealName(model.getRealName());
            if(updateUserId!=null){
                user.setUpdateUser(updateUserId);
            }
            user.setUpdateDate(new Date());
            return userDao.update(user);
        }
        return null;
    }
    @Transactional
    public Boolean countUserNameOrUserMobile(String code, Long userId) {
        String sql = "SELECT COUNT(1) FROM USER_USER WHERE ( USER_NAME='"+code+"' OR USER_MOBILE='"+code+"' ) ";
        if(userId!=null){
            sql+=" AND USER_ID != "+userId;
        }
        Integer count = userDao.countRecord(sql);
        if(count>0){return false;}else{return true;}
    }
    @Transactional
    public UserModel getUserBySearchParameter(SearchParameter searchParameter) {
        return userDao.getUserBySearchParameter(searchParameter);
    }
    @Transactional
    public UserModel findByAdminLoginId(String loginId) {
        SearchParameter searchParameter = new SearchParameter();
        searchParameter.addParam("name",loginId);
        return getUserBySearchParameter(searchParameter);
    }
    @Transactional
    public UserModel getUserModelByUserId(Long userId){
        return userDao.load(UserModel.class,userId);
    }
    @Transactional
    public void updatePassword(Long userId,String password,Long updateUserId){
        UserModel updateUser = getUserModelByUserId(userId);
        updateUser.setUserPassword(password);
        updateUser.setUpdateDate(new Date());
        updateUser.setUpdateUser(updateUserId);
        userDao.update(updateUser);
    }

    @Transactional
    public SearchPageResult<UserModel> getUserModelListPage(SearchParameter searchParameter) {
        return userDao.getUserModelListPage(searchParameter);
    }
}
